/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.dialog;

import cn.ac.nercis.pes.common.constant.evaluation.EvaluationConclusion;
import cn.ac.nercis.pes.community.component.controls.PesComboBox;
import cn.ac.nercis.pes.community.component.spreadsheet.analyze.UnitRecordSheetView;
import cn.ac.nercis.pes.community.utils.LogUtils;
import cn.ac.nercis.pes.community.utils.SpringUtils;
import cn.ac.nercis.pes.service.evaluation.UnitEvaluationService;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.StackPane;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;

/**
 * 单元指标测评记录列表弹框控制器
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class UnitRecordController implements Initializable {

    @FXML
    private StackPane page;
    @FXML
    private PesComboBox conclusionCbx;
    @FXML
    private UnitRecordSheetView sheetView;

    private final UnitEvaluationService unitEvaluationService;

    private String currentSystemId;
    private String currentUnitTargetId;

    public UnitRecordController(){
        this.unitEvaluationService = SpringUtils.getBean(UnitEvaluationService.class);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        this.conclusionCbx.getItems().clear();
        this.conclusionCbx.getItems().addAll("--全部--", EvaluationConclusion.MATCH.getName(),EvaluationConclusion.PARTIALLY_MATCH.getName(),EvaluationConclusion.NOT_MATCH.getName());
        this.conclusionCbx.getSelectionModel().selectedItemProperty().addListener((o, oldValue, newValue) -> {
            var conclusion = EvaluationConclusion.fromName(newValue);
            this.recordQuery(conclusion);
        });
    }

    public void initializeData(String systemId,String unitTargetId){
        this.currentSystemId = systemId;
        this.currentUnitTargetId = unitTargetId;
        LogUtils.info(log, "初始化单元指标测评记录列表...");
        this.conclusionCbx.getSelectionModel().select(0);
    }

    private void recordQuery(EvaluationConclusion conclusion) {
        LogUtils.info(log, "获取测评记录...");
        LogUtils.record(log, "请求数据，systemId：{},unitTargetId:{}", this.currentSystemId,
                this.currentUnitTargetId);
        CompletableFuture.supplyAsync(() -> this.unitEvaluationService.findEvaluationRecords(this.currentSystemId,
                        this.currentUnitTargetId))
                .thenAccept(res -> Platform.runLater(() -> {
                    if(conclusion == EvaluationConclusion.DEFAULT){
                        this.sheetView.loadContent(res);
                    }else{
                        this.sheetView.loadContent(res.stream()
                                .filter(er-> StringUtils.equals(er.getConclusion(),conclusion.getName()))
                                .toList());
                    }
                }))
                .exceptionally((ex) -> {
                    log.error("获取测评记录出错!", ex);
                    return null;
                });
    }

}